package com.ys.peony.ui.activity;


import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
import com.shizhefei.view.largeimage.LargeImageView;
import com.shizhefei.view.largeimage.factory.FileBitmapDecoderFactory;
import com.yklab.library.util.FileUtils;
import com.ys.peony.R;
import com.ys.peony.app.Cache;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.interfaces.OnDownloadListener;
import com.ys.peony.utils.ImageLoadUtils;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.ToastUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class ContractDetailActivity extends BaseActivity {
    @BindView(R.id.pdfview)
    PDFView pdfView;
    @BindView(R.id.view_blank)
    LinearLayout view_blank;
    @BindView(R.id.iv_image)
    LargeImageView iv_image;

    private ProgressDialog progressDialog;
    private String url;

    @Override
    protected int getContentResId() {
        return R.layout.activity_contract_detail;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null && intent.getExtras() != null) {
            Bundle bundle = intent.getExtras();
            url = bundle.getString(YSConstant.PARAM);
        }
        hideBlankView();
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast("请检查网络");
            return;
        }
        if (!url.contains(".pdf") && !url.contains(".png")) {
            ToastUtils.showToast("暂不支持该格式文件...");
            return;
        }
        if (url.contains(".pdf")) {
            pdfView.setVisibility(View.VISIBLE);
            iv_image.setVisibility(View.GONE);
            File file = new File(Cache.CONTRACT_MAIN);
            if (file == null || !file.exists()) {
                downContract();
            } else {
                loadContract(Cache.CONTRACT_MAIN);
            }
        } else if (url.contains(".png")) {
            iv_image.setVisibility(View.VISIBLE);
            pdfView.setVisibility(View.GONE);
            showContractImage();
        }

    }

    private void downContract() {
        /*progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("合同加载中");
        progressDialog.setMessage("请稍后...");
        progressDialog.setProgress(0);
        progressDialog.setMax(100);
        progressDialog.show();
        progressDialog.setCancelable(false);*/
        JCHttpManagerUtils jcHttpManagerUtils = new JCHttpManagerUtils();
        LoadingProgressUtils.showProgressDialog(this);
        jcHttpManagerUtils.downLoadFile(url, null, Cache.CONTRACT_MAIN, new OnDownloadListener() {
            @Override
            public void onDownloadSuccess(File file) {
                LoadingProgressUtils.closeDialog();
                loadContract(Cache.CONTRACT_MAIN);
                /*if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();

                }*/

            }

            @Override
            public void onDownloading(int progress) {
                //progressDialog.setProgress(progress);
            }

            @Override
            public void onDownloadFailed(Exception e) {
                LoadingProgressUtils.closeDialog();
                ToastUtils.showToast("网络异常");
            }
        });
    }

    private void showContractImage() {
        LoadingProgressUtils.showProgressDialog(this);
        Glide.with(this).load(url).downloadOnly(new SimpleTarget<File>() {
            @Override
            public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                LoadingProgressUtils.closeDialog();
                iv_image.setImage(new FileBitmapDecoderFactory(resource));
            }
        });

    }

    private void loadContract(String filePath) {
        File file = new File(filePath);
        if (file != null) {
            pdfView.fromFile(new File(filePath))
                    .enableSwipe(true) // allows to block changing pages using swipe
                    .swipeHorizontal(false)
                    .enableDoubletap(true)
                    .defaultPage(0)
                    .enableAntialiasing(true)
                    .onError(new OnErrorListener() {
                        @Override
                        public void onError(Throwable t) {
                            showBlankView();
                            ToastUtils.showToast("合同加载失败");
                        }
                    })
                    .load();
        } else {
            ToastUtils.showToast("合同下载失败");
        }
    }

    private void deleteContract() {
        FileUtils.doDeleteFile(Cache.CONTRACT_MAIN);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deleteContract();
    }

    private void showBlankView() {
        view_blank.setVisibility(View.VISIBLE);
        pdfView.setVisibility(View.GONE);
    }

    private void hideBlankView() {
        view_blank.setVisibility(View.GONE);
        pdfView.setVisibility(View.VISIBLE);
    }
}
